﻿package baseEngine.basic
{
    import baseEngine.system.*;
    import baseEngine.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.event.*;

    public class Viewer3D extends Scene3D
    {
        private var _out:Vector3D;
        private var _drag:Boolean;
        private var _spinX:Number = 0;
        private var _spinY:Number = 0;
        private var _spinZ:Number = 0;
        public var smooth:Number;
        public var speedFactor:Number;
        public var checkEventPhase:Boolean = true;
        public var canChangeRot:Boolean = true;
        public var canChangePos:Boolean = true;
        public var canChangeDistance:Boolean = true;

        public function Viewer3D(param1:DisplayObjectContainer, param2:Number = 1, param3:Number = 0.5, param4:Boolean = true)
        {
            this._out = new Vector3D();
            super(param1);
            if (param2 > 1)
            {
                throw new Error("Smooth value should be between 0 and 1.");
            }
            this.smooth = param2;
            this.speedFactor = param3;
            if (param4)
            {
                FrEventDispatcher.instance.proxyAddEventListener(this, Engine3dEventName.PIVOT3D_UPDATE, this.updateEvent, false, 0, true);
            }
            return;
        }// end function

        protected function updateEvent(event:Event) : void
        {
            var _loc_2:Number = NaN;
            if (!this.mouseEnabled)
            {
                return;
            }
            if (this.checkEventPhase && Input3D.eventPhase > EventPhase.AT_TARGET)
            {
                return;
            }
            if (Input3D.middleMouseUp)
            {
                this._drag = false;
            }
            if (this._drag)
            {
                if (Input3D.keyDown(Input3D.ALTERNATE) || Input3D.keyDown(Input3D.SPACE))
                {
                    this._spinX = this._spinX + Input3D.mouseXSpeed * this.smooth * this.speedFactor;
                    this._spinY = this._spinY + Input3D.mouseYSpeed * this.smooth * this.speedFactor;
                }
                else
                {
                    _loc_2 = camera.getPosition().length / 100;
                    _loc_2 = Math.pow(_loc_2, 1.3) / 50;
                }
            }
            if (Input3D.delta != 0 && viewPort.contains(Input3D.mouseX, Input3D.mouseY))
            {
                this._spinZ = (camera.getPosition(false, this._out).length + 0.1) * this.speedFactor * Input3D.delta / 20;
            }
            this._spinX = this._spinX * (1 - this.smooth);
            this._spinY = this._spinY * (1 - this.smooth);
            this._spinZ = this._spinZ * (1 - this.smooth);
            if (Input3D.middleMouseHit && viewPort.contains(Input3D.mouseX, Input3D.mouseY))
            {
                this._drag = true;
            }
            return;
        }// end function

        public function set spinZ(param1:Number) : void
        {
            this._spinZ = param1;
            return;
        }// end function

    }
}
